package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: RoleServiceImpl
 * @Author: jinghao
 * @Date: 2020/7/14 21:35
 * @Description:
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public void addRole(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.addRole(role);
        addRoleAndMenu(role.getId(), menuIds);
        addRoleAndPermission(role.getId(), permissionIds);

    }

    public void addRoleAndMenu(Integer roleId, Integer[] menuIds) {
        for (Integer menuId : menuIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("menuId", menuId);
            map.put("roleId", roleId);
            roleDao.addRoleAndMenu(map);
        }
    }

    public void addRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        for (Integer permissionId : permissionIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("permissionId", permissionId);
            roleDao.addRoleAndPermission(map);
        }
    }

    @Override
    public void update(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.update(role);
        roleDao.deleteRidAndMid(role.getId());
        roleDao.deleteRidAndPid(role.getId());
        addRoleAndMenu(role.getId(), menuIds);
        addRoleAndPermission(role.getId(), permissionIds);

    }

    @Override
    public void delete(Integer id) {
        roleDao.deleteRidAndMid(id);
        roleDao.deleteRidAndPid(id);
        Integer count = roleDao.findUidAndRid(id);
        if (count > 0) {
            throw new RuntimeException();
        }
        roleDao.delete(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        List<Role> result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, result);
    }

    @Override
    public List<Permission> findAllPermission() {
        return roleDao.findAllPermission();
    }

    @Override
    public List<Menu> findAllMenu() {
        return roleDao.findAllMenu();
    }

    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findRoleById(id);
    }

    @Override
    public Integer[] findMidsByRid(Integer id) {
        return roleDao.findMidsByRid(id);
    }

    @Override
    public Integer[] findPidsByRid(Integer id) {
        return roleDao.findPidsByRid(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
}
